package com.cy.store.controller;

import com.cy.store.service.ICartService;
import com.cy.store.utils.JsonResult;
import com.cy.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("carts")
public class CartController extends BaseController{

    @Autowired
    private ICartService cartService;

    @RequestMapping("add_to_cart")
    public JsonResult<Void> addToCart(HttpSession session,Integer pid,Integer amount){
        cartService.addToCart(getUidFromSession(session),pid,amount,getUserNameFromSession(session));
        return new JsonResult<>(OK);
    }

    @RequestMapping({"/",""})
    public JsonResult<List<CartVo>> findVOByUid(HttpSession session){
        List<CartVo> voByUid = cartService.getVOByUid(getUidFromSession(session));
        return new JsonResult<>(OK,voByUid);
    }

    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid , HttpSession session){
        Integer result = cartService.addNum(cid, getUidFromSession(session), getUserNameFromSession(session));
        return new JsonResult<>(OK,result);
    }

    @RequestMapping("list")
    public JsonResult<List<CartVo>> findVOByCids(Integer[] cids,HttpSession session){
        List<CartVo> list = cartService.getVOByCids(cids, getUidFromSession(session));
        return new JsonResult<>(OK,list);
    }
}
